package exam;

import java.util.ArrayList;
import java.util.HashSet;

public class ExamUse {

    /*
     *给定一个字符串，请你找出其中不含有重复字符的 最长子串 的长度。
     * */
    public static int lengthOfLongestSubstring(String s) {
        HashSet<Character> set=null;
        char[] chars = s.toCharArray();
        int maxLen = 0;
        for (int a=0;a<chars.length;a++){
            set =  new HashSet<>();
            int temp = 0;
            for (int b = a;b<chars.length;b++){
                if (set.contains(chars[b])){
                    break;
                }
                set.add(chars[b]);
                temp++;
            }
            maxLen = maxLen<temp? temp :maxLen;
        }
        return maxLen;
    }

    /*
     * 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,
     * 则按字典序打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
     * //递归思路：每次选一个字符  + 剩下未被选中字符的全排列 就是当前全排列
     * */
    public static ArrayList<String> Permutation(String str) {
        ArrayList<String> res = new ArrayList<>();
        if(str == null){
            return res;
        }
        if (str.length()==1){
            res.add(str);
            return res;
        }
        HashSet<String> stringHashSet = new HashSet<>();
        for (int a=0;a<str.length();a++){
            String strs = str;
            char t= strs.charAt(a);
            for (String s: Permutation(deleteIndex(strs,a))){
                String ss = t+s;
                res.add(ss);
                stringHashSet.add(ss);
            }
        }
        return res;
    }
    public static String deleteIndex(String string,int index){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(string.toCharArray());
        stringBuilder.deleteCharAt(index);
        return stringBuilder.toString();
    }

}
